<?php
class Word
{
	static public function camelize($str,$tolower = false)
	{
		$parts = preg_split('#[_]#',$str);
		$result = null;
		foreach($parts as $part)
		{
			$result .= ($result || !$tolower)?ucfirst(strtolower($part)):strtolower($part);
		}
		return $result;
	}
	
	static public function underscore($str)
	{
		preg_match_all('#([A-Z][a-z]+)#',$str,$matches);
		$result = null;
		foreach($matches[1] as $part)
		{
			$result .= $result ? '_' . strtolower($part) : strtolower($part);
		}
		return $result;
	}
	
	static public function pluralize($str)
	{
		$pattern = array('#ty\b#','#ess\b#');
		$replace = array('tie','esse');
		$str = preg_replace($pattern,$replace,$str);
		return $str.'s';
	}
	
	static public function indent($str,$nbr=1)
	{
		$t = null;
		for($i=0;$i<$nbr;$i++) { $t .= "\t"; }
		$str = $t.$str;
		return preg_replace('#\n#',"\n".$t,$str);
	}
	
	static public function colorSyntax($str)
	{
		$pattern = array(
					'#((class)|((static )?(public|private)? function)|(private)|(return)|(int)|(string)|(extends)|(implements)|(parent)'
					.'|(isset)|(foreach)|(while)|(\sas\s)|(if)|(else)|(require(_once)?)|(null)|(array)|(const\b)|(new))#',
					'#(\$\w+)#',
					"#('[^']*')#",
					"#(//.*)#",
					"#((->[a-zA-Z0-9_]+)+)#"
					);
		$replace = array(
					'<span class="reference">$1</span>',
					'<span class="variable">$1</span>',
					'<span class="string">$1</span>',
					'<span class="comment">$1</span>',
					'<span class="property">$1</span>'
					);
		return preg_replace($pattern,$replace,$str);
	}
}